//
//  LocalController.m
//  LocalTest
//
//  Created by Pibinuo on 15/12/4.
//  Copyright © 2015年 皮比诺. All rights reserved.
//

#import "LocalController.h"
#import "LocalListHelper.h"
#import "LocalLIst.h"

@interface LocalController ()
@property (nonatomic, strong) NSArray * sortArray;

@end

@implementation LocalController
- (NSArray *)sortArray
{
    if (_sortArray == nil) {
        self.sortArray = [NSArray array];
    }
    return _sortArray;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"Baikal.jpg"]];
    if (self.localCity == nil) {
        self.navigationItem.title = @"正在定位";
    } else {
        self.navigationItem.title = [NSString stringWithFormat:@"所在的城市是：%@",self.nowCity];
    }
    [[LocalListHelper defaultHelpers] loadData];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(finishListdata) name:@"finishList" object:nil];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"guanbi"] style:(UIBarButtonItemStyleDone) target:self action:@selector(backAction:)];
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell" ];
    [self addHeaderView];
    
    self.tableView.sectionIndexColor = [UIColor colorWithRed:0 green:0 blue:1 alpha:1];
    
}


//城市列表加载完成
-(void)finishListdata
{
    NSArray * array = [[LocalListHelper defaultHelpers].dataDict allKeys];
    self.sortArray = [array sortedArrayUsingSelector:@selector(compare:)];
    [self.tableView reloadData];

}

-(void)backAction:(UIBarButtonItem *)sender
{
    [[LocalListHelper defaultHelpers].dataDict removeAllObjects];
    [self dismissViewControllerAnimated:YES completion:nil];
}

-(void)addHeaderView
{
    UIView * view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 90)];
//    view.backgroundColor =[UIColor redColor];
    UILabel * lable = [[UILabel alloc] initWithFrame:CGRectMake(20, 10, CGRectGetWidth(self.view.frame) - 20, 30)];
    lable.backgroundColor = [UIColor clearColor];
    lable.text = @"可选频道";
    lable.font = [UIFont systemFontOfSize:12];
    lable.textColor = [UIColor grayColor];
    [view addSubview:lable];
    UIView * citybgView = [[UIView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(lable.frame), CGRectGetWidth(self.view.frame), 45)];
//    citybgView.backgroundColor = [UIColor brownColor];
    [view addSubview:citybgView];
    
    UILabel * cityLable = [[UILabel alloc] initWithFrame:CGRectMake(15, CGRectGetMaxY(lable.frame), CGRectGetWidth(self.view.frame) - 30, 45)];
    cityLable.backgroundColor = [UIColor whiteColor];
    cityLable.text = self.localCity;
    [view addSubview:cityLable];
    self.tableView.tableHeaderView = view;

}







- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    return [[LocalListHelper defaultHelpers] modelCount];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return [[LocalListHelper defaultHelpers].dataDict[self.sortArray[section]] count];
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    NSArray * cityArray = [LocalListHelper defaultHelpers].dataDict[self.sortArray[indexPath.section]];
    LocalLIst * local = cityArray[indexPath.row];
    cell.textLabel.text = local.name;
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    
    cell.backgroundColor = [UIColor colorWithRed:1 green:1 blue:1 alpha:0.3];
    
    return cell;
}

-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
        return self.sortArray[section];
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSArray * array = [LocalListHelper defaultHelpers].dataDict[self.sortArray[indexPath.section]];
    LocalLIst *local = array[indexPath.row];
    if (self.delegate != nil && [self.delegate respondsToSelector:@selector(passValue:)]) {
        [self.delegate passValue:local];
    }
    [[LocalListHelper defaultHelpers].dataDict removeAllObjects];
    [self dismissViewControllerAnimated:YES completion:nil];
                       
}

-(NSArray<NSString *> *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
    return self.sortArray;
}

-(NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index
{
    [tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:index] atScrollPosition:UITableViewScrollPositionTop animated:YES];
    return index;
}







@end
